
package com.ordern.classification;

import java.io.IOException;

import net.htmlparser.jericho.Config;
import net.htmlparser.jericho.LoggerProvider;
import net.htmlparser.jericho.Source;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.OrFilter;
import org.htmlparser.filters.TagNameFilter;

/**
 * @author manishmaheshwari
 * @version 1.0 Jul 20, 2011
 */
public class HtmlHelper {
	/**
	 * Deprecated. Use cleanseJerico.
	 * 
	 * @param htmlText
	 * @return
	 * @throws IOException
	 */
	// public static String cleanseHtml ( String htmlText ) throws IOException {
	// HtmlCleaner cleaner = new HtmlCleaner();
	// CleanerProperties props = cleaner.getProperties();
	// TagNode node = cleaner.clean( htmlText );
	// CompactHtmlSerializer htmlSerializer = new CompactHtmlSerializer( props );
	// return htmlSerializer.getAsString( node );
	// }
	
	public static String cleanseJerico ( String htmlText ) {
		Config.LoggerProvider = LoggerProvider.DISABLED;
		Source source = new Source( htmlText );
		String renderedText = source.getRenderer().toString();
		return renderedText;
	}
	
	/**
	 * Returns correct meta data, that has desc & keywords as value of NAME
	 * attribute.
	 * 
	 * @return
	 */
	public static NodeFilter correctMetaFilter () {
		TagNameFilter filter0 = new TagNameFilter();
		filter0.setName( "META" );
		HasAttributeFilter filter1 = new HasAttributeFilter();
		filter1.setAttributeName( "NAME" );
		filter1.setAttributeValue( "DESCRIPTION" );
		HasAttributeFilter filter2 = new HasAttributeFilter();
		filter2.setAttributeName( "NAME" );
		filter2.setAttributeValue( "KEYWORDS" );
		NodeFilter[] array0 = new NodeFilter[2];
		array0[ 0 ] = filter1;
		array0[ 1 ] = filter2;
		OrFilter filter3 = new OrFilter();
		filter3.setPredicates( array0 );
		NodeFilter[] array1 = new NodeFilter[2];
		array1[ 0 ] = filter0;
		array1[ 1 ] = filter3;
		AndFilter filter4 = new AndFilter();
		filter4.setPredicates( array1 );
		NodeFilter[] array2 = new NodeFilter[1];
		array2[ 0 ] = filter4;
		return filter4;
	}
	
	
}
